package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.atguigu.jxc.service.DamageService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报损
 */
@RestController
@RequestMapping("/damageListGoods")
public class DamageController {
    @Autowired
    private DamageService damageService;
    @Autowired
    private UserController userController;
    @Autowired
    private DamageListGoodsService damageListGoodsService;


    /**
     *查询报损单商品信息
     */
    @PostMapping("/goodsList")
    public Map<String,Object> goodsList(@RequestParam("damageListId") Integer damageListId){
        List<DamageListGoods> list = damageListGoodsService.goodsList(damageListId);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",list);

        return map;
    }


    /**
     * 报损单查询
     */
    @PostMapping("/list")
    public Map<String,Object> list(@Param("sTime") String sTime,
                                   @Param("eTime") String eTime){
        List<DamageList> list = damageService.list(sTime,eTime);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }

    /**
     * 新增报溢单
     */
    @PostMapping("/save")
    public ServiceVO save(@RequestParam("damageNumber") String damageNumber,
                          @RequestParam("damageDate") String damageDate,
                          @RequestParam("remarks") String remarks,
                          @RequestParam("damageListGoodsStr") String damageListGoodsStr,
                          HttpSession session){
        Map<String,Object> map = userController.loadUserInfo(session);
        List<String> list = new ArrayList<>();
        for (String key : map.keySet()) {
            list.add((String) map.get(key));
        }
        Integer userID = userController.getUserId(list);
        damageService.save(damageNumber,damageDate,remarks,damageListGoodsStr,userID);
        ServiceVO serviceVO = new ServiceVO(100,"请求成功",null);
        return serviceVO;
    }
}
